<template>
  <div class="addArticle">
    <el-header style="line-height:60px"><h2>选择题录入</h2></el-header>
        <div class="card kjfs">
          <el-row>
            <el-col :span="24">
              <p class="title" style="padding:25px"><i class="fa fa-th-large fa-lg"></i>选择题录入表数据</p>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18" :offset="2">
              <el-form ref="form" :rules="rules"
                                  :model="form" 
                                  label-width="120px" 
                                  @submit.native.prevent
                                  class="demo-dynamic"
                                  action="#">
                <el-form-item label="章节ID:"
                              prop="chapter_id">
                  <el-input-number v-model="form.chapter_id"></el-input-number>
                </el-form-item>
                <el-form-item label="课程ID：" prop="lesson_id">
                  <el-input-number v-model="form.lesson_id"></el-input-number>
                </el-form-item>
                <el-form-item label="选择题目：" prop="title">
                  <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="选择题图片：">
                  <el-upload
                    class="upload-demo"
                    action="http://exam.mujiayiling.com/api/uploadImage"
                    :on-preview="handlePreview"
                    :on-remove="handleRemove"
                    :before-remove="beforeRemove"
                    :on-success="uploadFile"
                    v-model="form.image"
                    multiple
                    :limit="3"
                    :on-exceed="handleExceed"
                    :file-list="fileList">
                    <el-button size="small" type="primary" >点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                  </el-upload>
                </el-form-item>
                <el-form-item label="建议分值：">
                  <el-input-number v-model="form.score"></el-input-number>
                </el-form-item>
                <el-form-item label="题目难度等级：">
                  <el-select v-model="form.difficulty"  prop="difficulty" placeholder="请选择题目难度等级">
                    <el-option label="简单" value="简单" selected></el-option>
                    <el-option label="一般" value="一般"></el-option>
                    <el-option label="较难" value="较难"></el-option>
                    <el-option label="困难" value="困难"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="选项答案：" prop="answer">
                  <el-input v-model="form.answer"></el-input>
                </el-form-item>
                <el-form-item label="选项A：" prop="option_a">
                  <el-input v-model="form.option_a"></el-input>
                </el-form-item>
                <el-form-item label="选项B：" prop="option_b">
                  <el-input v-model="form.option_b"></el-input>
                </el-form-item>
                <el-form-item label="选项C：" prop="option_c">
                  <el-input v-model="form.option_c"></el-input>
                </el-form-item>
                <el-form-item label="选项D：" prop="option_d">
                  <el-input v-model="form.option_d"></el-input>
                </el-form-item>
                <el-form-item label="选项E：">
                  <el-input v-model="form.option_e"></el-input>
                </el-form-item>
                <el-form-item label="题目分析：">
                  <el-input v-model="form.analysis"></el-input>
                </el-form-item>
                <el-form-item label="题目分析的图片：">
                  <el-upload
                    class="upload-demo"
                    action="http://exam.mujiayiling.com/api/uploadImage"
                    :on-preview="handlePreview"
                    :on-remove="handleRemove"
                    :before-remove="beforeRemove"
                    :on-success="analysisImag"
                    v-model="form.analysis_image"
                    multiple
                    :limit="3"
                    :on-exceed="handleExceed"
                    :file-list="fileList1">
                    <el-button size="small" type="primary" >点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                  </el-upload>
                </el-form-item>
                <el-form-item label="题目来源：">
                  <el-input v-model="form.source"></el-input>
                </el-form-item>
                <el-form-item label="是否可用：">
                  <el-switch v-model="form.is_del"></el-switch>
                </el-form-item>
                <el-form-item label="备注：">
                  <el-input type="textarea" v-model="form.remark"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="button" @click="onSubmit('form')">立即增加</el-button>
                  <el-button>取消</el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </div>
  </div>
</template>

<script>
import Markdown from '../../components/markdown/markdown-editor'
import initData from '../../markData.js'
export default {
  name: 'addArticle',
  data () {
    return {
      fileList:[],
      fileList1:[],
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      rules: {
        chapter_id: [
          { required: true, message: '章节ID不能为空'}
        ],
        lesson_id: [
          { required: true, message: '课程ID不能为空'}
        ],
        answer: [
          { required: true, message: '答案不能为空'}
        ],
        title: [
          { required: true, message: '题目不能为空'}
        ],
        difficulty: [
          { required: true, message: '难度不能为空'}
        ],
        option_a: [
          { required: true, message: '选项A不能为空'}
        ],
        option_b: [
          { required: true, message: '选项B不能为空'}
        ],
        option_c: [
          { required: true, message: '选项C不能为空'}
        ],
        option_d: [
          { required: true, message: '选项D不能为空'}
        ]
      },
      initData: initData,
      restaurants: [],
      form:{
          chapter_id:"",
          lesson_id:"",
          title:"",
          image:"",
          score:"",
          difficulty:"",
          answer:"",
          option_a:"",
          option_b:"",
          option_c:"",
          option_d:"",
          option_e:"",
          analysis:"",
          analysis_image:"",
          source:"",
          is_del:"",
          remark:""
        }
    }
  },
  components: {Markdown},
  methods: {
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePreview (file) {
      console.log(file)
    },
    handleExceed (files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    onSubmit(form){
      this.$refs[form].validate((valid) => {
          if (valid) {
            this.$http.post('/api/singlechoiceadd',this.form
                    //  this.qs.stringify(this.form),
                    //  {headers:{
                    //    'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'
                    //  }}
                     )
                .then(data=>{
                  if(data.data.code=='302'){
                      this.$message({
                      type: 'success',
                      message: data.data.message
                    });
                    this.$refs[form].resetFields();
                  }else{
                    this.$message({
                      type: 'error',
                      message: '创建失败'
                    });
                  }
                  
                }).catch(error=>{console.log(error)})
          } else {
            return false;
          }
        });
    },
    change () {
      console.log(arguments[0], this.article)
      this.article.content = arguments[0]
    },
    querySearch (queryString, cb) {
      var restaurants = this.restaurants
      var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter (queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    loadAll () {
      return [
        { 'value': 'vue' },
        { 'value': 'node.js' }
      ]
    },
    handleSelect (item) {
      console.log(item)
    },
    uploadFile(response, file, fileList){
      if(response.code=="1005"){
        this.$message({
          type: 'success',
          message: response.message
        });
        let uploadImg = {name:file.name,url:response.data}
        this.fileList.push(uploadImg)
        this.form.image=response.data
      }
    },
    analysisImag(response, file, fileList){
      if(response.code=="1005"){
        this.$message({
          type: 'success',
          message: response.message
        });
        let uploadImg = {name:file.name,url:response.data}
        this.fileList1.push(uploadImg)
        this.form.imanalysis_imageage=response.data
      }
    }
  },
  mounted () {
    this.restaurants = this.loadAll()
  }
}
</script>

<style lang="scss">
  .subBtn{
  width: 100px;
  margin: 0 auto;
}
</style>
